import React, { Component } from 'react'
import { ListBox } from '../../../components'
import { fetchData } from '../../../components/AjaxConsumer'
import EditPage from './EditPage'
import InfoPage from './Info'
import CalcPage from './Calc'
import ClosePage from './Close'
import OpenPage from './Open'
import CloseNewPage from './CloseNew'
import UpdatePage from './Update'
import { Modal } from 'antd';

export default class productOdsprostorage extends Component {
    constructor(props) {
        super(props);
        //行操作列表
        this.rowOperation = [
            {
                handleName: 'doubleClick',
                showName: '行双击事件'
            }
        ];
        //header 操作列表
        this.headerOperation = [
            {
                handleName: 'calc',
                showName: '调整分配数量',
                iconClass: 'edit',
                // permission: 'prostorage-calc'
            },
            {
                handleName: 'close',
                showName: '冻结',
                iconClass: 'edit',
                permission: 'prostorage-freeze'
            },
            {
                handleName: 'open',
                showName: '解冻',
                iconClass: 'edit',
                permission: 'prostorage-unfreeze'
            },
            {
                handleName: 'info',
                showName: '查看',
                iconClass: 'info'
            },
            {
                handleName: 'export',
                showName: 'Excel',
                iconClass: 'file-excel'
            }
        ];
        //table列表展示列配置
        //storageKey 本地存储的key
        //showName、columnName 必有属性， columnWidth 可选，默认100, columnHidden: true 列隐藏（用于列配置显示隐藏）, foreverHidden: true 列永久隐藏
        this.tableColumn = {
            storageKey: 'odsProStorageId',
            data: [
                {
                    showName: 'id',
                    columnName: 'odsProStorageId',
                    columnHidden: true,
                    foreverHidden: true
                },
                // {
                //     showName: '合同编号',
                //     columnName: 'contractNo',
                //     columnWidth: 100
                // }, {
                //     showName: '采购订单号',
                //     columnName: 'purchaseNo',
                //     columnWidth: 150
                // },
                {
                    showName: '型号',
                    columnName: 'model',
                    columnWidth: 100
                }, {
                    showName: '型号描述',
                    columnName: 'modelDesc',
                    columnWidth: 150
                }, {
                    showName: '型号类型',
                    columnName: 'modelType',
                    showOption: [//页面显示配置
                        {
                            optionKey: '0',
                            optionShow: '内销'
                        }, {
                            optionKey: '1',
                            optionShow: '外销'
                        }, {
                            optionKey: '2',
                            optionShow: '样机'
                        }
                    ]
                }, {
                    showName: '库存数量',
                    columnName: 'iventAmount',
                    columnWidth: 95
                }, {
                    showName: '可用数量',
                    columnName: 'availAmount',
                    columnWidth: 95
                }, {
                    showName: '总冻结数量',
                    columnName: 'lockAmount',
                    columnWidth: 95
                },{
                    showName: '仓库',
                    columnName: 'whCode',
                    columnWidth: 150
                }, {
                    showName: '区域',
                    columnName: 'wdCode',
                    columnWidth: 150
                }, {
                    showName: '库区',
                    columnName: 'waCode',
                    columnWidth: 150
                }, {
                    showName: '库位',
                    columnName: 'wlCode',
                    columnWidth: 150
                },
            ]
        };
        //搜索属性配置，如果字段不配置，默认是input搜索
        // 搜索类型：
        //      1、输入框 type='input'， 默认
        //      2、时间 type='date'
        //      3、数字 type='number'
        //      4、下拉框 type='select'
        //      5、下拉框 type='disabled'
        this.searchComponentData = [
            // {
            //     showName: '仓库',
            //     name: 'whCode',
            //     type: 'select',
            //     url: '/wmsrest/product/odsprostorage/getMdWareHouses',
            //     condition: { //查询条件

            //     }
            // },
            // {
            //     showName: '区域',
            //     name: 'wdCode',
            //     type: 'select',
            //     url: '/wmsrest/product/odsprostorage/getRegions',
            //     condition: { //查询条件

            //     }
            // },
            // {
            //     showName: '库区',
            //     name: 'waCode',
            //     type: 'select',
            //     url: '/wmsrest/product/odsprostorage/getMdAreas',
            //     condition: { //查询条件

            //     }
            // },
            {
                showName: '成品类型',
                name: 'productType',
                type: 'select',
                optionList: [//下拉框的信息
                    //下拉框的信息
                    {
                        optionKey: '',
                        optionShow: ''
                    }, {
                        optionKey: '0',
                        optionShow: '内销'
                    }, {
                        optionKey: '1',
                        optionShow: '外销'
                    }, {
                        optionKey: '2',
                        optionShow: '样机'
                    }
                ]
            },
            {
                showName: '库存数量',
                name: 'iventAmount',
                type: 'number',
                maxName: 'iventAmountMax', //最大值
                minName: 'iventAmountMin'  //最小值
            },
            {
                showName: '可用数量',
                name: 'availAmount',
                type: 'number',
                maxName: 'availAmountMax', //最大值
                minName: 'availAmountMin'  //最小值
            }, {
                showName: '总冻结数量',
                name: 'lockAmount',
                type: 'number',
                maxName: 'lockAmountMax', //最大值
                minName: 'lockAmountMin'  //最小值
            }, {
                showName: '已分配数量',
                name: 'allocAmount',
                type: 'number',
                maxName: 'allocAmountMax', //最大值
                minName: 'allocAmountMin'  //最小值
            }
        ];
    }
    rowHandles = (strHandles, rowObj) => {
        if (strHandles === 'calc') {
            this.calcHandle(rowObj);
        } else if (strHandles === 'close') {
            this.closeHandle(rowObj);
        } else if (strHandles == 'doubleClick') {
            this.infoHandle(rowObj);
        } else if (strHandles == 'open') {
            this.openHandle(rowObj);
        } else if (strHandles == 'info') {
            this.infoHandle(rowObj);
        } else if (strHandles == 'export') {
            this.exportXlsHandle(rowObj);
        }
    };

    exportXlsHandle = (conditonData) => {
        console.log('-------导出条件--------', conditonData);
        let param = '?'
        for (let key in conditonData) {
            if (conditonData[key] != '') {
                console.log(key, conditonData[key]);
                param = param + key + '=' + conditonData[key] + '&'
            }
        }
        console.log(param);
        window.location.href = '/wmsrest/product/odsprostorage/list1/exp' + param;
    }

    // 调整分配数量
    calcHandle = (rowObj) => {
        if (rowObj.length == 0) {
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
            return;
        } else if (rowObj.length > 1) {
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
            return;
        } else {
            // let pstData = {};
            // let tablesourse = [];
            // tablesourse[0] = rowObj[0].data;
            // this.refs.calcPage.editHandle(tablesourse, pstData);

            let pstData = {};
            pstData.wlCode = rowObj[0].data.wlCode;
            pstData.materialCode = rowObj[0].data.materialCode;
            pstData.productType = rowObj[0].data.productType;
            pstData.wlCode = rowObj[0].data.wlCode;
            pstData.allocAmount = rowObj[0].data.allocAmount;
            const options = {
                url: '/wmsrest/product/odsprostorage/adjust/allAMount/query',
                //url: '/wmsrest/product/odsprostoragesn/r/detail/',
                type: 'GET',
                condition: pstData,
            };
            fetchData(this, options, (resData) => {
                this.refs.calcPage.editHandle(resData.data, pstData);
            });
        }
    }

    closeHandle = (rowObj) => {
        if (rowObj.length == 0) {
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
            return;
        } else if (rowObj.length > 1) {
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
            return;
        } else {
            let pstData = {};
            pstData.wlCode = rowObj[0].data.wlCode;
            pstData.materialCode = rowObj[0].data.materialCode;
            pstData.productType = rowObj[0].data.productType;
            pstData.stauts = '0';
            const options = {
                url: '/wmsrest/product/odsprostoragesn/r/detail/',
                type: 'GET',
                condition: pstData,
            };
            fetchData(this, options, (resData) => {
                this.refs.closeNewPage.editHandle(resData, rowObj[0].data.odsProStorageId,
                    pstData.wlCode, pstData.materialCode, pstData.productType, pstData.stauts);
            });
        }
    };
    openHandle = (rowObj) => {
        if (rowObj.length == 0) {
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
            return;
        } else if (rowObj.length > 1) {
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
            return;
        } else {
            let pstData = {};
            pstData.wlCode = rowObj[0].data.wlCode;
            pstData.materialCode = rowObj[0].data.materialCode;
            pstData.productType = rowObj[0].data.productType;
            pstData.stauts = '1';
            console.log("rowObj[0].data.odsProStorageId:****************" + rowObj[0].data.odsProStorageId);
            const options = {
                url: '/wmsrest/product/odsprostoragesn/r/detailopen/',
                type: 'GET',
                condition: pstData,
            };
            fetchData(this, options, (resData) => {
                this.refs.openPage.editHandle(resData, rowObj[0].data.odsProStorageId,
                    pstData.wlCode, pstData.materialCode, pstData.productType, pstData.stauts);
            });
        }
    };

    infoHandle = (rowObj) => {
        if (rowObj.length == 0) {
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
            return;
        } else if (rowObj.length > 1) {
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
            return;
        } else {
            let pstData = {};
            pstData.wlCode = rowObj[0].data.wlCode;
            pstData.materialCode = rowObj[0].data.materialCode;
            pstData.productType = rowObj[0].data.productType;
            const options = {
                url: '/wmsrest/product/odsprostoragesn/r/detail/',
                type: 'GET',
                condition: pstData,
            };
            fetchData(this, options, (resData) => {
                this.refs.infoPage.editHandle(resData, pstData);
            });
        }
    };
    //刷新列表
    refreshHandle = () => {
        this.refs.listOdsprostorage.refreshHandle();
    };
    render() {
        //当左侧隐藏时，列表宽度加上左侧的宽度，
        //1、当前this.props.isHideLeft 是从页面组件containers/Main/main.js传过来
        //2、this.props.isHideLeft 最顶级赋值是在页面 containers/Main/main.js
        let isHideLeft = this.props.isHideLeft;
        return (
            <div>
                <ListBox
                    ref='listOdsprostorage'
                    title="成品库存查询"
                    isHideLeft={isHideLeft}
                    tableColumnData={this.tableColumn}
                    searchComponentData={this.searchComponentData}
                    url='/product/insidewarehouse/proStorage/list'
                    rowHandles={this.rowHandles}
                    rowOperation={this.rowOperation}
                    headerOperation={this.headerOperation}
                    isRowSingle={true}
                    subtractHeight='15'
                />
                <UpdatePage
                    ref="updatePage"
                    refreshHandle={this.refreshHandle}
                />
                <ClosePage
                    ref="closePage"
                    refreshHandle={this.refreshHandle}
                />
                <OpenPage
                    ref="openPage"
                    refreshHandle={this.refreshHandle}
                />

                <CloseNewPage
                    ref="closeNewPage"
                    refreshHandle={this.refreshHandle}
                />

                <InfoPage
                    ref="infoPage"
                    refreshHandle={this.refreshHandle}
                />

                <CalcPage
                    ref="calcPage"
                    refreshHandle={this.refreshHandle}
                />
            </div>
        )
    }
}